
#import <QuartzCore/QuartzCore.h>
#import "FindLampResultViewController.h"
#import "HelpViewController.h"
#import "Data.h"
#import "lampmeta.h"
#import "FindLampResultWebViewController.h"
#import "ComparePriceViewController.h"
#import "ResultCell.h"
#import "UIViewController+ModalWithNavBar.h"
#import "UINavigationItem+InfoButton.h"
#import "NSString+VoiceOver.h"
#import "UIFont+BoldFont.h"
#import "CalculationDetailsViewController.h"

@interface FindLampResultViewController () <UITableViewDelegate, UITableViewDataSource, UIWebViewDelegate, ResultCellDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UILabel *headerLabel;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UIView *tableHeaderView;
@property (strong, nonatomic) IBOutlet UILabel *tableHeaderViewLabel;

@end


@implementation FindLampResultViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;

    self.navigationItem.title = _(@"Resultat");
    
    [self.navigationItem addInfoButtonWithTarget:self action:@selector(showInfo)];
    
    NSString *typeString = self.savingsCalculator.oldLampCalculation.lampTypeKey;
    self.titleLabel.text = [NSString stringWithFormat:_(@"%@ lm"), [[Data sharedInstance] lumenStringFromWattValue:self.savingsCalculator.oldLampCalculation.effect lampTypeWithKey:typeString]];
    self.titleLabel.accessibilityLabel = [self.titleLabel.text stringByOptimizingForVoiceOver];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorColor = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)showInfo
{
    HelpViewController *helpVC = [[HelpViewController alloc] initWithNibName:nil bundle:nil];
    helpVC.documentFile = _(@"ResultList");
    helpVC.title = _(@"Information");
    [self presentModalWithNavBar:helpVC];
}

- (NSString *)energyImageNameForLampWithEnergyClass:(NSString *)energyClass;
{
    NSString *energyImageName = nil;
    if ([energyClass isEqualToString:@"A++-A"]) {
        energyImageName = @"ic-energi-aplusplus-a.png";
    } else if ([energyClass isEqualToString:@"A+-A"]) {
        energyImageName = @"ic-energi-aplus-a.png";
    } else if ([energyClass isEqualToString:@"B"]) {
        energyImageName = @"ic-energi-b";
    } else if ([energyClass isEqualToString:@"C"]) {
        energyImageName = @"ic-energi-c";
    } else if ([energyClass isEqualToString:@"C-D"]) {
        energyImageName = @"ic-energi-c-d";
    }
    return energyImageName;
}

- (NSString *)energyMarkNameForLampWithEnergyClass:(NSString *)energyClass
{
    NSString *energyImageName = nil;
    if ([energyClass isEqualToString:@"A++-A"]) {
        energyImageName = @"Energiklass A++ till A";
    } else if ([energyClass isEqualToString:@"A+-A"]) {
        energyImageName = @"Energiklass A+ till A";
    } else if ([energyClass isEqualToString:@"B"]) {
        energyImageName = @"Energiklass B";
    } else if ([energyClass isEqualToString:@"C"]) {
        energyImageName = @"Energiklass C";
    } else if ([energyClass isEqualToString:@"C-D"]) {
        energyImageName = @"Energiklass C till D";
    }
    return energyImageName;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 117.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.tableHeaderView.frame.size.height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.tableHeaderView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.savingsCalculator.substitutes.count;
}

#define kResultCellNibName @"ResultCell"
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ResultCell *cell = (ResultCell *)[tableView dequeueReusableCellWithIdentifier:kResultCellNibName];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:kResultCellNibName owner:self options:nil][0];
        cell.delegate = self;
        [cell initCell];
    }
    cell.tag = indexPath.row;
    SavingsCalculatorLampType *item = self.savingsCalculator.substitutes[indexPath.row];
    cell.lampLabel.text = item.name;
    
    float defaultPrice = [self.savingsCalculator defaultPriceForSubstituteAtIndex:indexPath.row];
    float price = [self.savingsCalculator purchasePriceForSubstituteAtIndex:indexPath.row];
    float savings = [self.savingsCalculator savingsForSubstituteAtIndex:indexPath.row];
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:defaultPrice == price ? @"Inköpspris: " : @"Ändrat inköpspris: "];
    NSDictionary *boldAttributes = @{ NSFontAttributeName:cell.lampPriceLabel.font.boldFont };

    SavingsCalculatorLampCalculation *lampCalculation = self.savingsCalculator.substCalculation[indexPath.row];
    NSString *priceUnit = lampCalculation.lampsNeeded > 1.0 ? @"kr/st" : @"kr";
    [text appendAttributedString: [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.0f %@", price, priceUnit] attributes:boldAttributes]];
    
    cell.lampPriceLabel.attributedText = text;
    cell.lampPriceLabel.accessibilityLabel = cell.lampPriceLabel.text.stringByOptimizingForVoiceOver;
    cell.energyMark.image = [UIImage imageNamed:[self energyImageNameForLampWithEnergyClass:item.energyClass]];
    cell.energyMark.accessibilityLabel = [self energyMarkNameForLampWithEnergyClass:item.energyClass];
    cell.savingsLabel.text = [NSString stringWithFormat:@"%.0f kr", savings];
    cell.savingsLabel.accessibilityLabel = [cell.savingsLabel.text stringByOptimizingForVoiceOver];
    cell.yearLabel.hidden = [self.savingsCalculator savingsForSubstituteAtIndex:indexPath.row] <= 0;
    cell.yearLabel.text = [NSString stringWithFormat:@"på %ld år", (long)self.savingsCalculator.calculationYears];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.savingsCalculator.substitutes.count) {
        SavingsCalculatorLampType *item = self.savingsCalculator.substitutes[indexPath.row];
        FindLampResultWebViewController *findLampResultWebVC = [[FindLampResultWebViewController alloc] initWithDefaultNibAndDocumentName:item.documentName];
        findLampResultWebVC.backButtonTitle = _(@"Tillbaka");
        findLampResultWebVC.headerViewLabel = item.name;
        findLampResultWebVC.title = _(@"Resultat");
        findLampResultWebVC.subTitle = self.titleLabel.text;
        [self.navigationController pushViewController:findLampResultWebVC animated:YES];
    }
}

#pragma mark - ResultCell Delegate
-(void)resultCell:(ResultCell *)resultCell changeLampPriceForSubstituteAtIndex:(NSInteger)index
{
    ComparePriceViewController *compVC = [[ComparePriceViewController alloc] initWithSavingsCalculator:self.savingsCalculator];
    compVC.subtituteIndex = index;
    SavingsCalculatorLampType *lampType = self.savingsCalculator.substitutes[index];
    compVC.lampString = lampType.name;
    compVC.title = _(@"Ändra pris");
    [self presentModalWithNavBar:compVC];
}

-(void)resultCell:(ResultCell *)resultCell showCalculationDetailsForSubstituteAtIndex:(NSInteger)index
{
    CalculationDetailsViewController *detailsVC = [[CalculationDetailsViewController alloc] initWithSavingsCalculator:self.savingsCalculator
                                                                                                      substituteIndex:index];
    
    [self presentModalWithNavBar:detailsVC];
}

@end
